<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <style>
        ul {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        h2 {
            margin: 0;
        }

        #wrap {
            margin: 30px auto;
            width: 482px;
            padding: 5px;
            position: relative;
            border: 1px solid #000;
            background: #eee;
        }

        .title {
            font: bold 18px/40px "宋体";
            text-align: center;
            border-bottom: 1px solid #000;
        }

        #list {
            padding-left: 2px;
        }

        #list li {
            display: flex;
            opacity: 0.8;
            font: 14px/36px "宋体";
            border-bottom: 1px solid #000;
        }

        #list li a {
            padding: 0 20px;
        }

        #list li:hover {
            opacity: 1;
        }

        #list label input {
            margin: 0 20px 0 30px;
        }

        input[type="chekbox"] {
            width: 20px;
            height: 20px;
        }

        .footer {
            font: 16px/36px "宋体";
        }
    </style>
</head>

<body>
    <section id="wrap">
        <h2 class="title">百度音乐榜单</h2>
        <ul id="list">
            <li>
                <input type="checkbox" checked="false" />
                <span>第一条信息</span>
                <a href="javascript:;" class="collect">收藏</a>
                <a href="javascript:;" class="cancelCollect">取消收藏</a>
                <a href="javascript:;" class="remove">删除</a>
            </li>
        </ul>
        <footer class="footer">
            <label><input type="checkbox" id="checkAll" />全选/全不选</label>
            <a href="javascript:;" id="remove">删除</a>
            <input type="text" id="newInfo" />
            <a href="javascript:;" id="add">添加</a>
        </footer>
    </section>
    <script>
        var data = [
            {
                id: 0,
                title: "残酷月光 - 费启鸣",
                checked: false,
                collect: false
            }, {
                id: 1,
                title: "兄弟 - 艾热",
                checked: false,
                collect: false
            }, {
                id: 2,
                title: "毕生 - 夏增祥",
                checked: false,
                collect: true
            }, {
                id: 3,
                title: "公子向北去 - 李春花",
                checked: true,
                collect: false
            }, {
                id: 4,
                title: "战场 - 沙漠五子",
                checked: false,
                collect: false //是否收藏 true 收藏 false 没有收藏
            }
        ];

        // -----------------------------------------------------------------------------------
        // 封装一个判断底部是否全选函数
        function isAll(data) {
            let num = 0;
            data.forEach(item => {
                num += item.checked;
                if (num === data.length && data.length > 0) {
                    checkAll.checked = true;
                } else {
                    checkAll.checked = false;
                }
            })
        }
        //    获取元素 渲染视图
        let checkAll = document.querySelector('#checkAll');
        let listEl = document.querySelector('#list');
        randerDom(data)
        function randerDom(data) {
            listEl.innerHTML = '';
            data.forEach(function (item, index) {
                let liEL = document.createElement('li');
                liEL.innerHTML = `
                    <input type="checkbox" ${item.checked ? 'checked' : ''} />
                    <span>${item.title}</span>
                    ${item.collect ? '<a href="javascript:;" class="collect">收藏</a>' : '<a href="javascript:;" class="cancelCollect">取消收藏</a>'}
                    <a href="javascript:;" class="remove">删除</a>
                `
                // 绑定事件 
                liEL.onclick = function (e) {
                    if (e.target.classList.contains('collect')) {
                        data[index].collect = !data[index].collect;
                        randerDom(data);
                    }
                    if (e.target.classList.contains('cancelCollect')) {
                        data[index].collect = !data[index].collect;
                        randerDom(data);
                    }
                    if (e.target.classList.contains('remove')) {
                        data.splice(index, 1);
                        randerDom(data);
                    }
                    if (e.target.nodeName === 'INPUT') {
                        data[index].checked = e.target.checked;
                        // 判断是否勾选底部全选按钮
                        isAll(data);
                        randerDom(data);
                    }
                }
                listEl.appendChild(liEL);
            })
        }
        // 底部功能实现
        // 全选按钮
        checkAll.onclick = function () {
            flag = checkAll.checked;
            data.forEach(item => item.checked = flag);
            randerDom(data);
            if (data.length === 0) {
                checkAll.checked = ''
            }
        }
        // 删除按钮 
        let removeEl = document.querySelector('.footer #remove');
        removeEl.onclick = function () {
            data = data.filter(item => item.checked != true)
            randerDom(data);
            if (data.length === 0) {
                checkAll.checked = ''
            }
        }

        //  添加按钮
        let addEl = document.querySelector('.footer #add');
        let newInfo = document.querySelector('.footer #newInfo');
        addEl.onclick = function () {
            let newVal = newInfo.value;
            if (!newVal.trim()) {
                alert('憨批好好输入内容');
                return
            }
            data.unshift({
                id: data.length,
                title: newVal,
                checked: false,
                collect: false
            })
            randerDom(data);
            newInfo.value = ''
        }


    </script>
</body>

</html>